﻿using ESMAM.DAL.SYS;
using ESMAM.Model.SYS;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ESMAM.BLL.SYS
{
    public class UserBLL
    {
        private static UserDAL dalUser = new UserDAL();
        public static Retful Login(string loginName, string pwd)
        {
            try
            {
                var user = dalUser.Login(loginName, pwd);
                if (user == null)
                {
                    return Retful.CreateFail("用户名或密码错误！");
                }

                if (user.state == 1)
                {
                    return Retful.CreateFail("该账号已被禁用！");
                }

                return Retful.CreateSuccess(user);
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.RawMsg());
            }
        }

        public static Retful ChangePassWd(string id, string password)
        {
            try
            {
                bool isOk = dalUser.ChangePassWd(id, password);
                if (isOk)
                {
                    return Retful.CreateSuccess(null, "密码修改成功！");
                }
                else
                {
                    return Retful.CreateFail("未知原因，密码修改失败！");
                }
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
        }


        public static Retful GetPager(NameValueCollection nvc)
        {
            Pager pager = null;
            try
            {
                pager = dalUser.GetPager(new ReqParam(nvc));
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
            return Retful.CreateSuccess(pager);
        }

        public static Retful SelectTable(NameValueCollection nvc)
        {
            DataTable data = null;
            try
            {
                data = dalUser.SelectTable(new ReqParam(nvc));
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
            return Retful.CreateSuccess(data);
        }

        public static Retful Upsert(NameValueCollection nvc)
        {
            try
            {
                //SYS_USER user = GetUser(nvc["id"]).Value;
                //if (user != null 
                //    && user.LOGINNAME.Equals("admin", StringComparison.OrdinalIgnoreCase) 
                //    && !nvc["loginname"].Equals("admin", StringComparison.OrdinalIgnoreCase))
                //{
                //    return Retful.CreateFail("不能对超级管理员登录名进行修改！");
                //}
                var kv = dalUser.Upsert(nvc);

                return Retful.CreateRetful(kv.Key ? 1 : -1, kv.Value);
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
        }

        public static Retful GetUser(string id)
        {
            sys_user user = null;
            try
            {
                user = dalUser.GetUser(id);
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
            return Retful.CreateSuccess(user);
        }

        public static Retful Delete(string id)
        {
            int res = -1;
            try
            {
                sys_user user = GetUser(id).Value;
                if (user != null && user.loginname.Equals("admin", StringComparison.OrdinalIgnoreCase))
                {
                    return Retful.CreateFail("不能对超级管理员用户删除！");
                }
                res = dalUser.Delete(id);
            }
            catch (Exception ex)
            {
                return Retful.CreateFail(ex.Message);
            }
            return Retful.CreateSuccess(null, "删除成功！");
        }
    }
}
